package cn.jly.flink.operators;

import cn.jly.flink.entity.Employee;
import cn.jly.flink.utils.ConstantUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.math.BigDecimal;

/**
 * @PackageName cn.jly.flink.operators
 * @ClassName KeyByDemo
 * @Description KeyBy测试
 * @Author 姬岚洋
 * @Date 2021/1/9 下午 11:14
 */
public class KeyByDemo {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.fromElements(
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), RandomStringUtils.random(5), BigDecimal.valueOf(10000), 1),
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), RandomStringUtils.random(5), BigDecimal.valueOf(20000), 2),
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), RandomStringUtils.random(5), BigDecimal.valueOf(30000), 1),
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), RandomStringUtils.random(5), BigDecimal.valueOf(40000), 2)
        )
                .keyBy(new KeySelector<Employee, Integer>() {
                    @Override
                    public Integer getKey(Employee employee) throws Exception {
                        return employee.getDeptId();
                    }
                })
                .print();

        env.execute("keyBy demo");
    }
}
